package com.binance.connector.client;

import com.alibaba.fastjson.JSONObject;
import com.binance.connector.client.impl.SpotClientImpl;
import com.binance.connector.client.utils.DateTimeUtils;
import com.binance.connector.client.utils.DateUtils;
import com.binance.connector.client.utils.JSONParser;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public final class NewOrder {
    private NewOrder() {
    }
    public static SpotClientImpl client = null;
    public static long recvWindow = 1250;

    /**
     * 根据资金量抢币
     * @param client
     * @throws InterruptedException
     */
    public static void buyCoinByQty(SpotClientImpl client){
        String result = "";
        //抢币
        LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("symbol", PrivateConfig.SYMBOL);
        parameters.put("side", "BUY");
        parameters.put("type", "MARKET");
        parameters.put("quoteOrderQty", PrivateConfig.QTYBUY);
        try {
            result = client.createTrade().newOrder(parameters);
            System.out.println(result);
            System.out.println("请求后"+DateUtils.dateTimeNow());
        } catch (Exception e) {
            System.err.println((String) String.format("fullErrMessage: %s", e.getMessage()));
            System.out.println("请求后"+DateUtils.dateTimeNow());
        }
    }

    /**
     * 根据资金量抢币
     * @param client
     * @throws InterruptedException
     */
    public static String sysTime(SpotClientImpl client){
        String result = "";
        result = client.createTrade().sysTime();
        System.out.println(result);
        return JSONObject.parseObject(result).getString("serverTime");
    }
    /**
     * 获取指定时间对应的毫秒数
     *
     * @param time "HH:mm:ss"
     * @return
     */
    private static long getTimeMillis(String time)
    {
        try
        {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss:SSS");
            DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
            Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
            return curDate.getTime();
        }
        catch (ParseException e)
        {
        }
        return 0;
    }
    public static void main(String[] args){
//        client = new SpotClientImpl(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY, PrivateConfig.BASE_URL);
//        if (client != null){
//            System.out.println("抢币客户端连接建立成功！！");
//        }
//        System.out.println("binance systime"+ DateTimeUtils.formatDateTimeString(Long.valueOf(sysTime(client))));
//        buyCoinByQty(client,Long.valueOf(args[0]));
        long oneDay = 24 * 60 * 60 * 1000;
        long initDelay = getTimeMillis(args[0]) - System.currentTimeMillis()-Long.valueOf(args[1]);//提前多少毫秒
        initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
        client = new SpotClientImpl(PrivateConfig.API_KEY, PrivateConfig.SECRET_KEY, PrivateConfig.BASE_URL);
        if (client != null){
            System.out.println("抢币客户端连接建立成功！！");
        }
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(60);
        long initvalue = 250;
        long[] arrray =new long[60];
        for(int i =0;i<arrray.length;i++){
            arrray[i]=initvalue;
            initvalue+=20;
        }
        //1
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay,oneDay,TimeUnit.MILLISECONDS);
        //2
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[0],oneDay,TimeUnit.MILLISECONDS);
        //3
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[1],oneDay,TimeUnit.MILLISECONDS);
        //4
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[2],oneDay,TimeUnit.MILLISECONDS);
        //5
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[3],oneDay,TimeUnit.MILLISECONDS);
        //6
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[4],oneDay,TimeUnit.MILLISECONDS);
        //7
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[5],oneDay,TimeUnit.MILLISECONDS);
        //8
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[6],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[7],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[8],oneDay,TimeUnit.MILLISECONDS);
        //10
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[9],oneDay,TimeUnit.MILLISECONDS);
        //11
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[10],oneDay,TimeUnit.MILLISECONDS);

        //3
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[11],oneDay,TimeUnit.MILLISECONDS);
        //4
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[12],oneDay,TimeUnit.MILLISECONDS);
        //5
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[13],oneDay,TimeUnit.MILLISECONDS);
        //6
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[14],oneDay,TimeUnit.MILLISECONDS);
        //7
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[15],oneDay,TimeUnit.MILLISECONDS);
        //8
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[16],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[17],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[18],oneDay,TimeUnit.MILLISECONDS);
        //10
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[19],oneDay,TimeUnit.MILLISECONDS);
        //11
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[20],oneDay,TimeUnit.MILLISECONDS);

        //3
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[21],oneDay,TimeUnit.MILLISECONDS);
        //4
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[22],oneDay,TimeUnit.MILLISECONDS);
        //5
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[23],oneDay,TimeUnit.MILLISECONDS);
        //6
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[24],oneDay,TimeUnit.MILLISECONDS);
        //7
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[25],oneDay,TimeUnit.MILLISECONDS);
        //8
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[26],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[27],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[28],oneDay,TimeUnit.MILLISECONDS);
        //10
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[29],oneDay,TimeUnit.MILLISECONDS);
        //11
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[30],oneDay,TimeUnit.MILLISECONDS);

        //3
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[31],oneDay,TimeUnit.MILLISECONDS);
        //4
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[32],oneDay,TimeUnit.MILLISECONDS);
        //5
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[33],oneDay,TimeUnit.MILLISECONDS);
        //6
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[34],oneDay,TimeUnit.MILLISECONDS);
        //7
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[35],oneDay,TimeUnit.MILLISECONDS);
        //8
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[36],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[37],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[38],oneDay,TimeUnit.MILLISECONDS);
        //10
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[39],oneDay,TimeUnit.MILLISECONDS);
        //11
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[40],oneDay,TimeUnit.MILLISECONDS);

        //3
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[41],oneDay,TimeUnit.MILLISECONDS);
        //4
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[42],oneDay,TimeUnit.MILLISECONDS);
        //5
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[43],oneDay,TimeUnit.MILLISECONDS);
        //6
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[44],oneDay,TimeUnit.MILLISECONDS);
        //7
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[45],oneDay,TimeUnit.MILLISECONDS);
        //8
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[46],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[47],oneDay,TimeUnit.MILLISECONDS);
        //9
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[48],oneDay,TimeUnit.MILLISECONDS);
        //10
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[49],oneDay,TimeUnit.MILLISECONDS);
        //11
        ses.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                buyCoinByQty(client);
            }
        }, initDelay+arrray[50],oneDay,TimeUnit.MILLISECONDS);
    }
}
